package com.pengcheng.hnweb.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.DataOutputStream;
import java.net.Socket;

@Slf4j
@Component
public class SocketUtil {
    @Value("${remote.ip}")
    private String ip;
    @Value("${remote.port}")
    private int port;
    @Autowired
    private Common common;
    @Autowired
    private FileUtil fileUtil;
    public boolean send(String msg)
    {
        try {
            Socket socket = new Socket(ip,port);
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            out.write(common.hex2byte(msg));
            out.flush();
            out.close();
            socket.close();
            return true;
        }
        catch (Exception ex)
        {
            fileUtil.add_msg("dsend",common.format_exception(ex));
            return false;
        }
    }
}
